<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>返回数组的所有子集</title>
</head>
<body>
  <script>
    function fn(nums) {
      let res = [];
      // 记录走过的路径;
      let track = [];
      const backtrack = (nums, start, track) => {
        res.push([...track]);
        // 注意 i 从 start 开始递增
        for (let i = start; i < nums.length; i++) {
          // 做选择
          track.push(nums[i]);
          // 回溯 基于选这个数，继续递归，传入的是i+1，不是start+1
          backtrack(nums, i + 1, track);
          // 撤销选择
          track.pop();
        }
      };
      backtrack(nums, 0, track)
      return res
    }
    console.log(fn([1, 2, 3]))
  </script>
</body>
</html>